﻿
using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Oxite.Configuration;
using Oxite.Infrastructure;
using ECube.SEC.Models;
using ECube.SEC.Filters;
using ECube.SEC.Services;
using ECube.SEC.Repositories;
using ECube.SEC.Repositories.SqlServer;

namespace ECube.SEC
{
    public class SECModule : IOxiteModule, IOxiteDataProvider
    {
        private readonly IUnityContainer container;

        public SECModule(IUnityContainer container)
        {
            this.container = container;
        }

        #region IOxiteModule Members

        public void Initialize(OxiteModuleConfigurationElement module)
        {
        }

        public void Unload()
        {
        }

        public void RegisterRoutes(RouteCollection routes)
        {
            string[] controllerNamespaces = new string[] { "ECube.SEC.Controllers" };

            routes.MapRoute(
                "Permission",
                "Admin/Permission/{action}/{id}",
                new { controller = "Security", action = "Permission", id = UrlParameter.Optional, AllowAnonymous = true },
                null,
                controllerNamespaces
                );

            routes.MapRoute(
                "ADUser",
                "Admin/ADUser/{action}",
                new { controller = "ADUser", action = "Find" },
                null,
                controllerNamespaces
                );
           
            //routes.MapRoute(
            //    "Registration",
            //    "Registration",
            //    new { controller = "User", action = "Registration" },
            //    null,
            //    controllerNamespaces
            //    );
            //routes.MapRoute(
            //    "UserChangePassword",
            //    "Admin/Users/{userName}/ChangePassword",
            //    new { controller = "User", action = "ChangePassword", role = "Admin", validateAntiForgeryToken = true },
            //    null,
            //    controllerNamespaces
            //    );
        }

        public void RegisterCatchAllRoutes(RouteCollection routes)
        {
        }

        public void RegisterFilters(IFilterRegistry filterRegistry)
        {
            filterRegistry.Add(Enumerable.Empty<IFilterCriteria>(), typeof(ECubeAuthorizationFilter));

            //ControllerActionFilterCriteria criteria = new ControllerActionFilterCriteria();

            //criteria.AddMethod<UserController>(u => u.ChangePassword(null));
            //criteria.AddMethod<UserController>(u => u.ChangePasswordSave(null, null));
            
            //filterRegistry.Add(new[] { criteria }, typeof(AuthorizationFilter));
        }

        public void RegisterModelBinders(ModelBinderDictionary modelBinders)
        {
          
        }

        public void RegisterWithContainer()
        {
            container
                .RegisterType<IAuthorizeService, AuthorizeService>()
                .RegisterType<IADAuthenticationUserService, ADAuthenticationUserService>();
            //    .RegisterType<IFormsAuthentication, FormsAuthenticationWrapper>();
        }

        #endregion

        #region IOxiteDataProvider Members

        public void ConfigureProvider(OxiteConfigurationSection config, OxiteDataProviderConfigurationElement dataProviderConfig, IUnityContainer container)
        {
            string connStr = !string.IsNullOrEmpty(dataProviderConfig.ConnectionString) ? dataProviderConfig.ConnectionString : config.Providers.DefaultConnectionString;
           
            if (dataProviderConfig.Category == "LinqToSql")
               container               
                    .RegisterType<ECubeAuthorizeDataContext>(new InjectionConstructor(new ResolvedParameter<string>(connStr)))
                    .RegisterType<IAuthorizeRepository,SqlServerAuthorizeRepository>();
        }

        #endregion
    }
}
